package com.asa.hutils;
/**
 * 离散方法解二阶微分方程
 * @author Administrator
 *
 */
public class DiscretizationMethod {

	/**
	 * 解形如
	 * x'' = e^t-3*sin(t)+x'-x;
	 * x(1)=1.09737 491		x(2)=8.63749 661
	 * 
	 * 
	 */
	public static void main(String[] args) {
		int n = 99;
		double[] a = new double[n];
		double[] b = new double[n];
		double[] c = new double[n];
		double[] d = new double[n];
		double[] y = new double[n];
		double ta = 1;
		double tb = 2;
		double aa =  1.09737491;
		double bb = 8.63749661;
		double t;
		double h = (tb-ta)/(n);
		for (int i = 0; i < n; i++) {
			t = ta + (i+1)*h;
			a[i] =  -(1 + (h/2)*w(t));
			d[i] = 2 + h*h*v(t);
			c[i] = -(1 -(h/2)*w(t));
			b[i] = -h*h*u(t);
		}
		b[1] = b[1]-a[1]*aa;
		b[n-1] = b[n-1] - c[n-1]*bb;
		for (int i = 0; i < n-1; i++) {
			a[i] = a[i+1];
		}
		 y = Tri(n-1,a,d,c,b,y);//还未实现不知道内部方法
		 //这行以下内容属于误差值得计算
		double error = Math.exp(ta) - 3*Math.cos(ta)-aa;
		//System.out.println(ta+"   "+aa+"   "+error);
		for (int i = 0; i < n; i++) {
			t = ta+i*h;
			error = Math.exp(t) - 3*Math.cos(t)-y[i];
			//System.out.println(t+"   "+y[i]+"   "+error);
		}
		error = Math.exp(tb) - 3*Math.cos(tb)-bb;
		//System.out.println(tb+"   "+bb+"   "+error);
	}
	
	private static double[] Tri(int n, double[] a, double[] d, double[] c,
			double[] b, double[] y) {
//		//System.out.println("=========a==========="+a.length);
//		for (int i = 0; i <	a.length; i++) {
//			//System.out.println(a[i]);
//		}
//		//System.out.println("...........d............"+d.length);
//		for (int i = 0; i < d.length; i++) {
//			//System.out.println(d[i]);
//		}
//		//System.out.println("...........c............"+c.length);
//		for (int i = 0; i < c.length; i++) {
//			//System.out.println(c[i]);
//		}
//		//System.out.println("...........b............"+b.length);
//		for (int i = 0; i < b.length; i++) {
//			//System.out.println(b[i]);
//		}
//		//System.out.println("====================");
		
		double A[][] = new double[n][n];
		double B[] = new double[n];
		
		for (int i = 1; i < n; i++) {
			d[i] = d[i]-(a[i-1]/d[i-1])*c[i-1];
			b[i] = b[i] - (a[i-1]/d[i-1])*b[i-1];
		}
		y[n-1] = b[n-1]/d[n-1];
		
		for (int i = n-2; i >=0; i--) {
			y[i]=(b[i]-c[i]*y[i+1])/d[i];
		}
		for (int i = 0; i < y.length; i++) {
			
			//System.out.println(y[i]);
		} 
		
		//System.out.println("====================");
		
		return  y;
	}

	public static double u(double x){
		return Math.exp(x)-3*Math.sin(x);
	}
	public static double v(double x){
		return -1;
	}
	public static double w(double x){
		return 1;
	}
	
	
	

}
